<?php

namespace App\ApiBackend\Menu;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\DB;

/**
 *  菜单选择列表
 */
class SelectList extends ApiBaseController
{

  /**
   * 参数检查
   */
  protected function check(): void
  {

  }

  /**
   * 业务主体
   * 最大深度为4级
   */
  protected function service(): void
  {
    //基础查询构造器
    $selectFields = [
      "id",
      "pid",
      "title",
    ];
    $listData = DB::table('admin_menu')
      ->select($selectFields)
      ->where('pid', 0)
      ->where('type', "<", 2)
      ->orderBy("orderNo", "DESC")
      ->get();
    foreach ($listData as &$item) {
      $item->children = DB::table('admin_menu')
        ->select($selectFields)
        ->where('pid', $item->id)
        ->where('type', "<", 2)
        ->orderBy("orderNo", "DESC")
        ->get();
      foreach ($item->children as &$childItem) {
        $childItem->children = DB::table('admin_menu')
          ->select($selectFields)
          ->where('pid', $childItem->id)
          ->where('type', "<", 2)
          ->orderBy("orderNo", "DESC")
          ->get();
      }
    }

    empty($listData) && $listData = [];

    $this->result["data"] = $listData;
    return;
  }
}
